package model;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;

import exception.DeleteFailureException;

public class PluginCleaner {
	private String gStartPath;

	private Hashtable<String, ArrayList<String>> gFilenames;
	private ArrayList<String> gCantRecorgFilenames;

	public PluginCleaner() {
		//
	}

	public void readAllPlugins(String path) {
		this.gStartPath = path;
		File dir = new File(path);

		if (dir.isDirectory()) {
			gFilenames = new Hashtable<String, ArrayList<String>>();
			String[] fn = dir.list();

			for (String cur_fn : fn) {
				if (!cur_fn.contains("_")) {
					if (gCantRecorgFilenames == null) {
						gCantRecorgFilenames = new ArrayList<String>();
					}

					if (gCantRecorgFilenames.contains(cur_fn)) {
						continue;
					}
					gCantRecorgFilenames.add(cur_fn);
				} else {
					String key = cur_fn.substring(0, cur_fn.indexOf('_'));
					ArrayList<String> a_plgunin = gFilenames.get(key);

					if (a_plgunin == null) {
						a_plgunin = new ArrayList<String>();
						gFilenames.put(key, a_plgunin);
						a_plgunin.add(cur_fn);
					} else {
						if (a_plgunin.contains(cur_fn)) {
							continue;
						}
						if (cur_fn.compareToIgnoreCase(a_plgunin.get(0)) < 0) {
							a_plgunin.add(cur_fn);
						} else if (cur_fn.compareToIgnoreCase(a_plgunin.get(0)) > 0) {
							a_plgunin.add(0, cur_fn);
						} else {
							// should not get in here
						}
					}
				}
			}
		} else {
			// should not get in here
		}
	}

	public String getGStartPath() {
		return gStartPath;
	}

	private static void removePlugin(String path) throws DeleteFailureException {
		File file = new File(path);
		if (file.isDirectory()) {
			String[] fns = file.list();
			for (String filename : fns) {
				removePlugin(path + "/" + filename);
			}
			file.delete();
		} else {
			if (!file.delete()) {
				throw new DeleteFailureException(file.getPath()
						+ file.getName());
			}
		}
	}

	public static void removeOldPlugins(String startPath,
			Hashtable<String, ArrayList<String>> filenames)
			throws DeleteFailureException {
		Set<String> keys = filenames.keySet();
		for (String key : keys) {
			ArrayList<String> fns = filenames.get(key);
			if (fns.size() > 1) {
				for (int i = 1; i < fns.size(); i++) {
					removePlugin(startPath + "/" + fns.get(i));
				}
			}
		}
	}

	public Hashtable<String, ArrayList<String>> getGFilenames() {
		return gFilenames;
	}

	public ArrayList<String> getGCantRecorgFilenames() {
		return gCantRecorgFilenames;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String gPath = "eclipse/features";

		PluginCleaner cleaner = new PluginCleaner();

		cleaner.readAllPlugins(gPath);

		Set<String> keys = cleaner.getGFilenames().keySet();

		for (String key : keys) {
			ArrayList<String> fns = cleaner.getGFilenames().get(key);
			if (fns.size() > 1) {
				log("KEY-" + key + ":");
				for (int i = 0; i < fns.size(); i++) {
					log("   " + fns.get(i));
				}
			}
		}

		if (cleaner.getGCantRecorgFilenames() != null) {
			for (String fn : cleaner.getGCantRecorgFilenames()) {
				log("###" + fn);
			}
		}

		/** Remove files and folders. */

		// try {
		// removeOldPlugins(gPath, cleaner.getGFilenames());
		// } catch (DeleteFailureException e) {
		// e.printStackTrace();
		// }
	}

	private static void log(String msg) {
		System.out.println(msg);
	}

}
